package io.awen.modules.sign.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.awen.common.exception.RRException;
import io.awen.common.utils.R;
import io.awen.modules.sign.entity.SignTaskOperationEntity;
import io.awen.modules.sign.service.SignTaskOperationService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;



/**
 * 任务操作记录
 *
 * @author liu.wen
 * @email liu.wen@qq.com
 * @date 2022-08-13 10:55:10
 */
@RestController
@RequestMapping("sign/signtaskoperation")
public class SignTaskOperationController {
    @Autowired
    private SignTaskOperationService signTaskOperationService;

    @ApiOperation("司机同意分配给自己的收运任务")
    @PostMapping("/agree")
//    @RequiresPermissions("sign:signtask:waitedlist")
    public R agree(@RequestBody SignTaskOperationEntity signTaskOperation){
        if (signTaskOperation.getTaskId() == null) {
            throw new RRException("请选择任务");
        }
        signTaskOperationService.agree(signTaskOperation);
        return R.ok();
    }

    @ApiOperation("司机拒绝分配给自己的收运任务")
    @PostMapping("/reject")
//    @RequiresPermissions("sign:signtask:waitedlist")
    public R reject(@RequestBody SignTaskOperationEntity signTaskOperation){
        if (signTaskOperation.getTaskId() == null) {
            throw new RRException("请选择任务");
        }
        signTaskOperationService.reject(signTaskOperation);
        return R.ok();
    }

    @ApiOperation("司机提交分配给自己的收运任务")
    @PostMapping("/submit")
//    @RequiresPermissions("sign:signtask:waitedlist")
    public R submit(@RequestBody SignTaskOperationEntity signTaskOperation){
        if (signTaskOperation.getTaskId() == null) {
            throw new RRException("请选择任务");
        }
        signTaskOperationService.submit(signTaskOperation);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{taskId}")
    public R list(@PathVariable("taskId") Long taskId){
        List<SignTaskOperationEntity> list = signTaskOperationService.list(
                new QueryWrapper<SignTaskOperationEntity>().lambda().eq(SignTaskOperationEntity::getTaskId, taskId)
        );

        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sign:signtaskoperation:info")
    public R info(@PathVariable("id") Long id){
		SignTaskOperationEntity signTaskOperation = signTaskOperationService.getById(id);

        return R.ok().put("signTaskOperation", signTaskOperation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sign:signtaskoperation:save")
    public R save(@RequestBody SignTaskOperationEntity signTaskOperation){
		signTaskOperationService.save(signTaskOperation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sign:signtaskoperation:update")
    public R update(@RequestBody SignTaskOperationEntity signTaskOperation){
		signTaskOperationService.updateById(signTaskOperation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sign:signtaskoperation:delete")
    public R delete(@RequestBody Long[] ids){
		signTaskOperationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
